package com.zhengdongzulin.zuke.base

import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import com.baidu.mapapi.SDKInitializer
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.lxj.androidktx.AndroidKTX
import com.lxj.androidktx.BuildConfig
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.zhengdongzulin.zuke.widget.GifRefreshHeader
import rxhttp.wrapper.param.RxHttp


/**
 * jeff
 * 2021-03-24 19:53:50
 */
class MyApp : ViewModelStoreOwner {

    private lateinit var mFactory: ViewModelProvider.Factory
    var mIsRelease = true

    companion object {
        public var mFragments: MutableList<Fragment> = mutableListOf()
        public var mActivitys: MutableList<Activity> = mutableListOf()
        public lateinit var mAppViewModelStore: ViewModelStore
        private var _context: Application? = null

        @JvmStatic
        fun onCreate(context: Application) {
            _context = context
            mAppViewModelStore = ViewModelStore()
            RxHttp.setDebug(true, true)

            AndroidKTX.init(
                context = context,
                isDebug = BuildConfig.DEBUG,
                defaultLogTag = "logTag",
                sharedPrefName = "ydshangpin"
            )
            SDKInitializer.initialize(context);
        }


        /**
         * radius   == -1   不做操作
         *          == 0    centerCrop
         *          ==指定像素圆角
         */
        fun getGlideOptions(resId: Int, radius: Int) =
            RequestOptions().apply {
                if (radius > 0) {
                    optionalTransform(RoundedCorners(radius))
                } else if (radius == 0) {
                    centerCrop()
                }
                if (resId > 0) {
                    placeholder(resId)
                    error(resId)
                }
                diskCacheStrategy(DiskCacheStrategy.ALL)
                skipMemoryCache(true)
            }

        fun getCircleGlideOptions(resId: Int) =
            RequestOptions().apply {
                circleCrop()
                if (resId > 0) {
                    placeholder(resId)
                    error(resId)
                }
                diskCacheStrategy(DiskCacheStrategy.ALL)
                skipMemoryCache(true)
            }

        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                //                    layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
                GifRefreshHeader(context) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        }
    }

    fun getContext(): Context {
        return _context!!
    }

    public fun getAppViewModelProvider(activity: Activity): ViewModelProvider {
        return ViewModelProvider(
            getViewModelStore(),
            getAppFactory(activity)
        )
    }

    private fun getAppFactory(activity: Activity): ViewModelProvider.Factory {
        val application = checkApplication(activity)
        mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
        return mFactory
    }

    private fun checkApplication(activity: Activity): Application {
        return activity.application
            ?: throw IllegalStateException(
                "Your activity/fragment is not yet attached to "
                        + "Application. You can't request ViewModel before onCreate call."
            )
    }

    public override fun getViewModelStore(): ViewModelStore {
        return mAppViewModelStore
    }

}